package com.tjetc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.vo.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.domain.Notice;
import com.tjetc.mapper.NoticeMapper;
import com.tjetc.service.NoticeService;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService{

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Notice record) {
        return noticeMapper.insert(record);
    }

    @Override
    public int insertSelective(Notice record) {
        return noticeMapper.insertSelective(record);
    }

    @Override
    public Notice selectByPrimaryKey(Integer id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Notice record) {
        return noticeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Notice record) {
        return noticeMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Notice> findAllNotice() {
        return noticeMapper.findAllNotice();
    }

    @Override
    public Result getById(Integer id) {
        Notice notice=noticeMapper.getById(id);
        return Result.ok(notice);
    }

    @Override
    public PageInfo<Notice> findByPage(String title, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> list=noticeMapper.findByPage(title);
        return new PageInfo<>(list);
    }

    @Override
    public Result addNotice(Notice notice) {
        if (notice.getId()==null){
            Date date=new Date();
            notice.setTime(date);
            int rows=noticeMapper.insert(notice);
            return Result.ok(rows);
        }else if (notice.getId()!=null){
            int rows=noticeMapper.updateByPrimaryKey(notice);
            return Result.ok(rows);
        }
        return Result.fail();
    }

    @Override
    public Result deleteNotice(Integer id) {
        int rows=noticeMapper.deleteByPrimaryKey(id);
        return Result.ok(rows);
    }

}
